package Eve.EveA;

import Eve.Eve;

import java.io.*;

public class EveA extends Eve {
    
    protected final static String    EVE_CIPHER_DIR  = ".\\eveA cipher text\\";

    public static void main(String[] args){
        System.out.println("EveA is started");        
        EveA e = new EveA();
        e.run();
    }

    // Object region

    /**
     * check for new files to attack under the MAIN_DIR
     */
    public void run(){
        //noinspection InfiniteLoopStatement
        while(true){
            File dir = new File(MAIN_DIR);
            String[] children = dir.list();
            for (String filename : children) {
                if (filename.endsWith(".txt") && !m_processedFiles.contains(filename)){
                    String name= filename.substring(0,filename.length()-4);
                    try {
                        copyFile(new File(CIPHER_DIR+"\\"+name+".ct"),new File(EVE_CIPHER_DIR+"\\"+name+".ct"));
                        copyFile(new File(MAIN_DIR+"\\"+name+".txt"),new File(EVE_CIPHER_DIR+"\\"+name+".txt"));
                        char[] key;
                        key = crack(name);
                        if (key != null){
                            saveKey(name,key);
                            m_processedFiles.add(filename);
                        }
                    }
                    catch (IOException e) {
                        System.out.println("Thecnical problem to access "+filename+", will retry later");
                    }
                }
            }
            //noinspection EmptyCatchBlock
            try {
                Thread.sleep(1000);
            }
            catch (InterruptedException e) {}
        }
    }

    /**
     * cracking the ciher text using compare method.
     * @param filename          The file to crack.
     * @return the cracked key
     * @throws IOException      A problem accessing the file
     */
    public char[] crack(String filename) throws IOException {
        File chiperFile = new File(CIPHER_DIR+"\\"+filename+".ct");
        if (!chiperFile.exists() || chiperFile.length() == 0)
            return null;       
        System.out.println("Attacking file: " + filename+" by KnownPlainText");
        File plainFile = new File(MAIN_DIR+"\\"+filename+".txt");
        char[] resKeys = new char[N];
        FileReader plain = new FileReader(plainFile);
        FileReader cipher = new FileReader(chiperFile);
        int c1 = plain.read();
        int c2 = cipher.read();
        int count=0;
        while(count<N && c1!=-1 && c2!=-1){
            if(c1>=START_SYMBOL && c1<=END_SYMBOL && resKeys[(c1-START_SYMBOL)]==0){
                resKeys[(c1-START_SYMBOL)]=(char)(c2);
                count++;                
            }
            c1 = plain.read();
            c2 = cipher.read();
        }

        plain.close();
        cipher.close();

        plainFile.delete();

        System.out.println("Done attacking file");
        return resKeys;
    }
}
